package com.example.understand_compose1.fet_dailypoetry

import android.os.Bundle
import android.util.Log
import androidx.activity.enableEdgeToEdge
import androidx.fragment.app.FragmentActivity
import com.example.understand_compose1.databinding.ActivityFetDailyPoetryBinding
import com.jinrishici.sdk.android.JinrishiciClient
import com.jinrishici.sdk.android.listener.JinrishiciCallback
import com.jinrishici.sdk.android.model.JinrishiciRuntimeException
import com.jinrishici.sdk.android.model.PoetySentence


class FetDailyPoetryActivity : FragmentActivity() {
    lateinit var binding: ActivityFetDailyPoetryBinding

    override fun onCreate(savedInstanceState: Bundle?) {

        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        binding = ActivityFetDailyPoetryBinding.inflate(layoutInflater)

        setContentView(binding.root)

        // ref links: https://github.com/xenv/jinrishici-sdk-android/
// ERROR: 主线程是不是不应该调用同步方法（阻塞）,否则闪退
//        val onePoetry = JinrishiciClient.getInstance().oneSentence
        val onepoetry = JinrishiciClient.getInstance().getOneSentenceBackground(
            object: JinrishiciCallback{
            override fun done(poetySentence: PoetySentence){
                // TODO:
                binding.jrsctv1.setDataFormat { poetrySentence ->
                    // TODO: return String by yourself
                    // TODO: see how to insert a interface by kotlin
                    "你的ip: ${poetrySentence.ipAddress} \n今日诗词:  ${poetrySentence.data.content}"
                }
            }

            override fun error(e: JinrishiciRuntimeException) {
                Log.d("FetDailyPoetryActivity", "ERROR: ${e.message},可能是ip被ban了，OnCreate失败")
//              TODO: 并不能设置text，为什么
            //                binding.jrsctv1.text = "ERROR: ${e.message},可能是ip被ban了"

            }
        })

//        INFO: java 处理逻辑
//        client.getOneSentenceBackground(object : JinrishiciCallback {
//            override fun done(poetySentence: PoetySentence) {
//                TODO do something
//                在这里进行你的逻辑处理
//            }
//            override fun error(e: JinrishiciRuntimeException) {
//                Log.w(TAG, "error: code = " + e.code + " message = " + e.message)
//                TODO do something else
//            }
//        })



    }
}